package com.ruoyi.api.controller;

import com.ruoyi.admin.domain.Question;
import com.ruoyi.admin.domain.UserAnswer;
import com.ruoyi.admin.model.response.QuestionInfoResp;
import com.ruoyi.admin.service.QuestionService;
import com.ruoyi.admin.service.UserAnswerService;
import com.ruoyi.api.model.request.AnswerReq;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;


@RestController
@RequestMapping("api/question")
public class ApiQuestionController extends BaseController {

    /**
     * 服务对象
     */
    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserAnswerService userAnswerService;

    /**
     * 分页查询题目
     * @param question 查询实体
     * @return 所有数据
     */
    @GetMapping("page")
    public TableDataInfo page(Question question) {
        List<QuestionInfoResp> questionPage = this.questionService.questionPage(buildPage(), question);
        return getDataTable(questionPage);
    }

    /**
     * 回答问题
     * 返回（0手动判断，1正确，2错误）
     * @RequestBody answerReq
     * @return
     */
    @PostMapping("answer")
    public AjaxResult answer(@RequestBody AnswerReq answerReq) {
        return AjaxResult.success(userAnswerService.answer(answerReq));
    }

    /**
     * 查看历史答题
     * @param questionId
     * @return
     */
    @PostMapping("answerHistory")
    public AjaxResult answerHistory(Long questionId) {
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setQuestionId(questionId);
        return AjaxResult.success(userAnswerService.answerHistory(userAnswer));
    }
}
